package com.gr.catanddog_web.controller;


import com.alibaba.fastjson2.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gr.catanddog_common.aspect.ApiOperationLog;
import com.gr.catanddog_common.damin.Animal;
import com.gr.catanddog_common.damin.AnimalImage;
import com.gr.catanddog_common.damin.AnimalLikes;
import com.gr.catanddog_common.damin.User;
import com.gr.catanddog_common.dao.AnimalClickMapper;
import com.gr.catanddog_common.dao.AnimalImageMapper;
import com.gr.catanddog_common.dao.AnimalLikesMapper;
import com.gr.catanddog_common.dao.AnimalMapper;
import com.gr.catanddog_common.utils.Pet;
import com.gr.catanddog_common.utils.Recommend;
import com.gr.catanddog_common.utils.Response;
import com.gr.catanddog_common.utils.Retrieve;
import lombok.extern.slf4j.Slf4j;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;

import static com.gr.catanddog_common.animalEnum.RetrieveEnum.*;

@RestController
@Slf4j
public class AnimalController {
    @Value("${web.url.path}")
    String webPath;
    @Autowired
    private AnimalImageMapper animalImageMapper;
    @Autowired
    private AnimalClickMapper animalClickMapper;
    @Autowired
    private AnimalMapper animalMapper;
    @Autowired
    SimpleDateFormat formatter;

    @Value("${file.upload.url}")
    private  String filePath;

    @PostMapping("/animalload")
    @ApiOperationLog(description = "宠物信息添加")
    public Response addAnimal(@RequestParam("animal") String str,@RequestParam("file") MultipartFile[] files) throws IOException {
        Animal pet = JSON.parseObject(str, Animal.class);
        System.out.println(pet);
        pet.setTime(formatter.format(new Date()));
        pet.setId(String.valueOf(new Date().getTime()));
        for(int i=0;i< files.length;i++){
            String fileName = String.valueOf(new Date().getTime())+".jpg";
            if(i==0){
                pet.setImage(webPath+fileName);
            }else{
                animalImageMapper.insert(new AnimalImage(pet.getId(),webPath+fileName));
            }
            File dest = new File(filePath+'/'+fileName);
            files[i].transferTo(dest);
        }
        pet.setStatus(false);
        pet.setLikes(0);
        animalMapper.insert(pet);
        return Response.success();
    }

    @PostMapping("/retrieve")
    @ApiOperationLog(description = "信息检索")
    public Response searcherRetrieve(@RequestParam("breed") Integer breed,
                                     @RequestParam("age")  Integer age,
                                     @RequestParam("weight")  Integer weight,
                                     @RequestParam("pageNum") Integer pageNum//,
                                     /*@RequestParam("token") String token*/){
        IPage<Animal> page1 = new Page<>(pageNum,16);
        LambdaQueryWrapper<Animal> lqw= new LambdaQueryWrapper<>();
        //Integer breed = form.getBreed();
        //System.out.println(form);
        if(ANIMAL_ADOPT.getValue()==breed){
        } else if (CAT_NO_ADOPT.getValue()==breed){
            lqw.like(Animal::getSpecies,CAT_NO_ADOPT.getName());
            lqw.eq(Animal::getIsAdopt,0);
        }else if(CAT_YES_ADOPT.getValue()==breed){
            lqw.like(Animal::getSpecies,CAT_YES_ADOPT.getName());
            lqw.eq(Animal::getIsAdopt,1);
        }else if(DOG_NO_ADOPT.getValue()==breed){
            lqw.like(Animal::getSpecies,DOG_NO_ADOPT.getName());
            lqw.eq(Animal::getIsAdopt,0);
        }else{
            lqw.like(Animal::getSpecies,DOG_YES_ADOPT.getName());
            lqw.eq(Animal::getIsAdopt,1);
        }
        //Integer age=form.getAge();
        if(ANIMAL_AGE.getValue()==age){
        } else if(AGE_FEW.getValue()==age){
            lqw.eq(Animal::getAge,AGE_FEW.getName());
        }else if(AGE_FEWER.getValue()==age){
            lqw.eq(Animal::getAge,AGE_FEWER.getName());
        }else if(AGE_LITTLE.getValue()==age){
            lqw.eq(Animal::getAge,AGE_LITTLE.getName());
        } else{
            lqw.eq(Animal::getAge,AGE_OLD.getName());
       }
        if(ANIMAL_WEIGHT.getValue()==weight){}
        else if(WEIGHT_FEW.getValue()==weight){lqw.eq(Animal::getWeight,WEIGHT_FEW.getName());}
        else if(WEIGHT_FEWER.getValue()==weight){lqw.eq(Animal::getWeight,WEIGHT_FEWER.getName());}
        else if(WEIGHT_LITTLE.getValue()==weight){lqw.eq(Animal::getWeight,WEIGHT_LITTLE.getName());}
        else lqw.eq(Animal::getWeight,WEIGHT_BIG.getName());
        IPage<Animal> animalIPage = animalMapper.selectPage(page1, lqw);
        return Response.success(animalIPage);
    }

    @GetMapping("/images")
    @ApiOperationLog(description = "轮播图片获取")
    public Response getImage(){
        LambdaQueryWrapper<Animal> lqw = new LambdaQueryWrapper<>();
        List<Animal> animal = animalMapper.selectList(
                lqw.select(Animal::getImage)
                .orderBy(true, false, Animal::getLikes)
                .last("limit 5")
        );
        List<String> images = new ArrayList<>();
        for (Animal animal1 : animal) {
            images.add(animal1.getImage());
        }
        return Response.success(images);
    }


    @GetMapping("/imagepage")
    @ApiOperationLog(description = "分页查询")
    public Response getAnimalPage(String token,Integer page){
        IPage<Animal> page1 = new Page<>(page,16);
        LambdaQueryWrapper<Animal> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Animal::getStatus,1);
        IPage<Animal> animalIPage = animalMapper.selectPage(page1, lqw);
        return  Response.success(animalIPage,token);
    }

    @GetMapping("/animalClick")
    @ApiOperationLog(description = "获取宠物点赞信息")
    public Response getAnimalClick(String token){
        LambdaQueryWrapper<Animal> lqw = new LambdaQueryWrapper<Animal>();


        return Response.success(token);
    }


    @GetMapping("/getrecommend")
    @ApiOperationLog(description = "获取推荐信息")
    public Response getRecommend(){
        List<Animal> cat = new ArrayList<>();
        List<Animal> dog = new ArrayList<>();
        LambdaQueryWrapper<Animal> lqw = new LambdaQueryWrapper<>();
        lqw.like(Animal::getSpecies,"猫")
                .orderBy(true,false,Animal::getLikes)
                .last("limit 5");
        cat = animalMapper.selectList(lqw);
        lqw.clear();
        lqw.select(Animal::getId,Animal::getAnimalName,Animal::getImage,Animal::getSex,Animal::getAge,Animal::getSpecies)
                .like(Animal::getSpecies,"狗")
                .or()
                .like(Animal::getSpecies,"犬")
                .orderBy(true,false,Animal::getLikes)
                .last("limit 5");
        dog = animalMapper.selectList(lqw);
        return Response.success(new Recommend(cat,dog));
    }
    @GetMapping("loadAnimal")
    @ApiOperationLog(description = "动物详情获取")
    public Response loadAnimal(@RequestParam("id")String id){
        LambdaQueryWrapper<Animal> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Animal::getId,id);
        Animal animal = animalMapper.selectOne(lqw);
        LambdaQueryWrapper<AnimalImage> lqw1 = new LambdaQueryWrapper<>();
        lqw1.eq(AnimalImage::getAnimalId,id);
        List<AnimalImage> animalImages = animalImageMapper.selectList(lqw1);
        List<String> animalUrl = new ArrayList<>();
        animalUrl.add(animal.getImage());
        for (AnimalImage animalImage : animalImages) {
            animalUrl.add(animalImage.getImageUrl());
        }
        animal.setAnimalUrl(animalUrl);
        return Response.success(animal);
    }
    @GetMapping("isAdopt")
    @ApiOperationLog(description = "是否领养")
    public Response isAdopt(@RequestParam("id")String id){
        LambdaQueryWrapper<Animal> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Animal::getId,id);
        Animal animal = animalMapper.selectOne(lqw);
        animal.setIsAdopt(!animal.getIsAdopt());
        animalMapper.update(animal, lqw);
        return Response.success();
    }
}
